<?php

namespace mbilling\BillingBundle\Model;

use Doctrine\ORM\EntityManager;
use mbilling\BillingBundle\Entity\orders;
use mbilling\BillingBundle\Entity\ordersShared;

Class OrdersModel
{
    protected $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function findAll()
    {
        $orders = $this->em->getRepository('BillingBundle:orders')->findAll();
        return $orders;
    }
    
    public function findByOrderName($order_name)
    {
        $orderShared = $this->em->getRepository('BillingBundle:ordersShared')->findOneBy(array('unix_login' => $order_name));
        $order = $this->findByPK($orderShared->getOrderId());
        return $order;
    }
    
    public function findByUserId($user_id)
    {
        $orders = $this->em->getRepository('BillingBundle:orders')->findBy(array('user_id' => $user_id));
        return $orders;
    }

    public function findByPK($order_id)
    {
        $order = $this->em->find('BillingBundle:orders', $order_id);
        return $order;
    }
    
    public function create($oEntity)
    {    
        $this->em->persist($oEntity);
        $this->em->flush();
    }

    public function removeByPK($order_id)
    {
        $order = $this->em->find('BillingBundle:orders', $order_id);
        $this->em->remove($order);
        $this->em->flush();
    } 
}
?>
